// 初始化测试数据库的脚本
const { exec } = require('child_process');
const path = require('path');
require('dotenv').config({ path: path.resolve(__dirname, '../.env.test') });

// 从环境变量获取数据库配置
const dbHost = process.env.DB_HOST || 'localhost';
const dbUser = process.env.DB_USER || 'root';
const dbPass = process.env.DB_PASS || '123456';
const dbName = process.env.DB_NAME || 'flower_db_test';

console.log('正在创建测试数据库:', dbName);

// 创建测试数据库的SQL命令
const createDbCommand = `mysql -h ${dbHost} -u ${dbUser} -p${dbPass} -e "CREATE DATABASE IF NOT EXISTS \`${dbName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"`;

exec(createDbCommand, (error, stdout, stderr) => {
  if (error) {
    console.error('创建测试数据库时出错:', error.message);
    return;
  }
  
  if (stderr) {
    console.error('stderr:', stderr);
    return;
  }
  
  console.log('测试数据库创建成功:', dbName);
  console.log('stdout:', stdout);
});